<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>02_计算器练习</title>
</head>
<body>
<input type="text" placeholder="请输入数字1" id="num1"><br>
<input type="text" placeholder="请输入数字2" id="num2"><br>
<button onclick="calc('+')">加</button>
<button onclick="calc('-')">减</button>
<button onclick="calc('*')">乘</button>
<button onclick="calc('/')">除</button>
<h4>运算结果是：<span></span></h4>
<script>
    function calc(op) {
      //1.获取用户输入的数字
        let num1 = document.querySelector('#num1').value;
        let num2 = document.querySelector('#num2').value;
        num1 = parseFloat(num1);
        num2 = parseFloat(num2);
        //2.获取用来显示结果的span元素
        let spanE = document.querySelector('span');
        //3.根据传入的运算符做运算
        switch (op) {
            case '+':
                spanE.innerHTML = num1 + num2;
                break;
            case '-':
                spanE.innerHTML = num1 - num2;
                break;
            case '*':
                spanE.innerHTML = num1 * num2;
                break;
            case '/':
                if (num2!=0){
                  spanE.innerHTML = num1 / num2;
                }else {
                  alert('除数不能为0');
                }
        }
    }
</script>
</body>
</html>